package com.szxy.hotel.controller;

import com.szxy.hotel.entity.Admin;
import com.szxy.hotel.service.AdminService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
public class AdminController {
    @Resource(name = "adminService")
    AdminService adminService;

    /**
     * 管理员注册
     */
    @RequestMapping("/regist.do")
    @ResponseBody
    public Map<String, Object> regist(String userAccount, String userPassword, String userAnswer, String selectType, String selectHobby, HttpServletRequest request){
        Map<String, Object> map = new HashMap<String, Object>();
        HttpSession session = request.getSession();
        //设置管理员的初始值  0表示登录，1表示未登录
        int userStatusid = 0;
        //查询是否存在该用户
        Admin admin = adminService.findByuserAccount(userAccount);
        //判断是否为空
        if(admin != null){
            map.put("status", 1);
            map.put("message", "该账号已存在");
        }else{
            //将数据插入到数据库
            int result = adminService.doRegist(userAccount,userPassword,userAnswer,selectType,selectHobby,userStatusid);
            if(result == 1){
                session.setAttribute("UserName",userAccount);
                session.setAttribute("UserType",selectType);
                map.put("status", 0);
                map.put("message", "注册成功");
            }else {
                map.put("status", 1);
                map.put("message", "注册失败");
            }
        }
        return map;
    }
    /**
     * 管理员登录
     */
    @RequestMapping("/login.do")
    @ResponseBody
    public Map<String, Object> login(String userAccount, String userPassword,String selectType,HttpServletRequest request){
        Map<String, Object> map = new HashMap<String, Object>();
        HttpSession session = request.getSession();
        //查询是否存在该用户
        Admin admin = adminService.findByuserAccount(userAccount);
        if(admin == null){
            map.put("status", 1);
            map.put("message", "该账号不存在");
        }else {
            //进一步判断账号的密码和类型是否正确
            if(userPassword.equals(admin.getUserPassword())&&selectType.equals(admin.getUserType())){
                session.setAttribute("UserName",userAccount);
                session.setAttribute("UserType",selectType);
                map.put("status", 0);
                map.put("message", "登录成功");
            }else {
                map.put("status", 1);
                map.put("message", "登录失败");
            }
        }
        return map;
    }
    /**
     * 找回密码显示提问
     */
    @RequestMapping("/retrieve.do")
    @ResponseBody
    public Map<String,Object> retrieve(String userAccount,HttpServletRequest request){
        Map<String, Object> map = new HashMap<String, Object>();
        HttpSession session = request.getSession();
        //查询是否存在该用户
        Admin admin = adminService.findByuserAccount(userAccount);
        if(admin == null){
            map.put("status", 1);
            map.put("message", "该账号不存在");
            map.put("result",null);
        }else {
            session.setAttribute("userAccount",userAccount);
            session.setAttribute("userQuestion",admin.getUserQuestion());
            map.put("status", 0);
            map.put("message", "查询成功");
            map.put("result",admin.getUserQuestion());
        }
        return map;
    }

    /**
     * 找回密码答案验证
     */
    @RequestMapping("/retrieveAnswer.do")
    @ResponseBody
    public Map<String, Object> retrieveAnswer(String userName,String userAnswer){
        Map<String, Object> map = new HashMap<String, Object>();
        //查询该账号
        Admin admin = adminService.findByuserAccount(userName);
        //判断该账号的答案是否正确
        if(admin.getUserAnswer().equals(userAnswer)){
            map.put("status", 0);
            map.put("message", "找回成功");
        }else {
            map.put("status", 1);
            map.put("message", "答案不正确，找回失败");
        }
        return map;
    }
    /**
     * 修改密码
     */
    @RequestMapping("/updatepassword.do")
    @ResponseBody
    public Map<String,Object> updatepassword(String userName,String userPassword,HttpServletRequest request){
        Map<String, Object> map = new HashMap<String, Object>();
        HttpSession session = request.getSession();
        //修改该账号的密码
        int result = adminService.doUpdatepassword(userName,userPassword);
        if(result == 1){
            session.invalidate();
            map.put("status", 0);
            map.put("message", "修改成功");
        }else {
            map.put("status", 1);
            map.put("message", "修改失败");
        }
        return map;
    }

    /**
     * 锁屏设置登录状态值
     */
    @RequestMapping("/toDecode.do")
    public String toDecode(HttpServletRequest request){
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("UserName");
        int result = adminService.doDecode(username);
        if(result == 1){
            Admin admin = adminService.findByuserAccount(username);
            session.setAttribute("UserStatusid",admin.getUserStatusid());
            return "decode";
        }else{
            return "index";
        }
    }
    /**
     * 解除锁屏恢复登录状态值
     */
    @RequestMapping("/decode.do")
    @ResponseBody
    public Map<String, Object> decode(String uname,String userPassword,HttpServletRequest request){
        Map<String, Object> map = new HashMap<String, Object>();
        HttpSession session = request.getSession();
        Admin admin = adminService.findByuserAccount(uname);
        if(userPassword.equals(admin.getUserPassword())){
            int result = adminService.overDecode(uname);
            if(result == 1){
                String username = (String) session.getAttribute("UserName");
                Admin admin1 = adminService.findByuserAccount(username);
                session.setAttribute("UserStatusid",admin1.getUserStatusid());
                map.put("status", 0);
                map.put("message", "解码成功");
            }else {
                map.put("status", 1);
                map.put("message", "解码失败");
            }
        }else {
            map.put("status", 1);
            map.put("message", "解码失败");
        }
        return map;
    }
    /**
     * 账号退出
     */
    @RequestMapping("/toQuit.do")
    public String toQuit(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.invalidate();
        return "redirect:toLogin.do";
    }
}
